package cn.lijiajia3515.cairo.system.modules.file2;

import cn.lijiajia3515.cairo.core.CoreConstants;
import cn.lijiajia3515.cairo.security.oauth2.jwt.CairoJwtPrincipal;
import cn.lijiajia3515.cairo.system.modules.file.FileConstant;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.Valid;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Collection;
import java.util.List;

@RestController
@RequestMapping("/api/file2")
public class File2Api {
	private final File2Service file2Service;

	public File2Api(File2Service file2Service) {
		this.file2Service = file2Service;
	}

	/**
	 * 获取 访问资源的url
	 *
	 * @param param 参数
	 * @return 访问urls
	 */
	@PostMapping("/get_objects_url")
	public List<String> getUrl(@RequestBody @Valid @Validated GetObjectsUrlParam param) {
		return file2Service.getObjectUrl(param);
	}

	/**
	 * 获取 上传资源签名
	 *
	 * @param param 参数
	 * @return 上传签名参数
	 */
	@PostMapping("/get_upload_objects_sign")
	public SignedArgs sign(@RequestBody @Valid @Validated SignFile2Param param) {
		return file2Service.getUploadObjectPresignedArgs(param);
	}

	/**
	 * 获取上传资源的url
	 *
	 * @param objects 资源值
	 * @return 上传资源的url
	 */
	@PostMapping("/get_upload_objects_url")
	public List<String> getUploadObjectsUrl(@RequestBody @Valid @Validated List<String> objects) {
		return file2Service.getSignUrl(objects);
	}

	/**
	 * 获取上传临时文件 的 url
	 *
	 * @param size 个数
	 * @return 上传url 有效期2H
	 */
	@PostMapping("temporary/get_upload_objects_url")
	public List<List<String>> temporaryUrl(@RequestParam(name = "Size", defaultValue = "1") Integer size) {
		return file2Service.getTemporarySignUrl(size);
	}

	/**
	 * 临时文件上传
	 *
	 * @param files 文件
	 * @return x
	 */
	@PostMapping("/upload_temporary_upload")
	@PreAuthorize("isAuthenticated()")
	public List<List<String>> temporaryUploads(
		@AuthenticationPrincipal CairoJwtPrincipal principal, @RequestPart(name = "files") @Valid @NotNull @NotEmpty Collection<MultipartFile> files) {
		String client = principal.getClient();
		String path = FileConstant.TEMPORARY_FILE_PATH.concat("/").concat(CoreConstants.SNOWFLAKE.nextIdStr());
		return file2Service.uploadObjects(client, path, files);
	}

	/**
	 * 文件信息 查询
	 *
	 * @param param 文件状态查询
	 * @return 文件信息 集合
	 */
	@PostMapping("/stat/find")
	public List<FileStat> findStat(@RequestBody @Valid @Validated StatFileParam param) {
		return file2Service.findStat(param);
	}

}
